/*eslint-disable*/
import {clearLocalStorage, mapGetLocalStorageKey} from "@/utils/localstore";
import {initializationRoutes, errorRouter} from "@/router";
import {getMenuList} from "@/api/menu/menu";
import {
    BaseLayout
} from '@/layout';

import {Home} from '@/views';

const renderAsyncComponent = (router) => {
    if (!router || !router.length) return [];
    return router.map((route) => {
        if(route.children && route.children.length) {
            const {children, redirect, ...props} = route;
            if(route.levelNum === 1) {
                route = {
                    ...props,
                    componentName: route.component,
                    component: () => import(`@/views/home/components/${props.component}`)
                }
            }
            renderAsyncComponent(route.children)
        }else {
            const {redirect, ...props} = route;
            if(route.levelNum === 1) {
                return {
                    ...props,
                    componentName: route.component,
                    component: () => import(`@/views/home/components/${route.component}`)
                };
            }
            return {
                ...props,
            };
        }
        return route;
    })
}


const state = {
    routes: []
};

const mutations = {
    setRoutes: (state, routes) => {
        state.routes = routes
    }
};

const actions = {
    getDefaultMenuList: ({commit}) => {
        const {roleId} = mapGetLocalStorageKey();
        return new Promise((resolve, reject) => {
            try {
                getMenuList({
                    roleId,
                    state: 4
                }).then(menuList => {
                    const {data, success} = menuList;
                    if (success) {
                        const filterChildrenRouter = renderAsyncComponent(data);
                        const finallyRouter = {
                            path: '/',
                            name: 'home',
                            component: BaseLayout,
                            redirect: filterChildrenRouter[0].path,
                            children: [
                                {
                                    meta: {
                                        title: '柯桥服务平台'
                                    },
                                    path: filterChildrenRouter[0].path,
                                    component: Home,
                                    redirect: `${
                                        filterChildrenRouter.filter((item, index) => {
                                            return !index && item.path
                                        })[0].path
                                    }`,
                                    children: filterChildrenRouter
                                },
                            ]
                        }
                        commit('setRoutes', initializationRoutes.concat(finallyRouter, errorRouter));
                        resolve([finallyRouter]);
                    }else {
                        resolve(errorRouter)
                    }
                })
            }catch (e) {
                reject(e)
            }
        })
    },
    actionRoutes: async ({dispatch}, roles) => {
        await dispatch('getDefaultMenuList');
        if (!roles.length) {
            clearLocalStorage();
        }
    }
};

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
